package com.unis.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Config {
	public static Document document = null;
	public static String url = "";
	public static void setUrl(String url){
		Config.url = url;
	}
	public static void readConfigFile() throws DocumentException {
		InputStream in = null;		
		try {
			in = new FileInputStream(new File(url));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("外部配置文件出错 使用默认配置");
			in = Config.class.getResourceAsStream("/DatabaseConfig.xml");
		}
		SAXReader saxReader = new SAXReader();
		document = saxReader.read(in);

	}

	public static String getConfigValue(String str) throws DocumentException {
		if(document==null){
			readConfigFile() ;
		}
		List list = document.selectNodes("//config/"+str);
		if (list.size() > 0) {
			Element element = (Element)list.get(0);
	    	return element.getText();
		}
		return null;
	}
}